let TopicDao = require('../dao/topicDao');
let CommentDao = require('../dao/commentDao');
let PraiseDao = require('../dao/praiseDao');
let UserDao = require('../dao/userDao');
const UserRightsDao = require('../dao/userRightsDao');
exports.create = function (params) {
    return new Promise(function (resolve, reject) {
        let insertTopicParams = {
            openid: params.openid,
            TopicOwnOpenId: params.TopicOwnOpenId,
            TopicType: params.TopicType,
            TopicContent: params.TopicContent,
            CreatedAt: new Date()
        };
        UserRightsDao.queryRights(masterPool, params)
            .then(function (result) {
                if (result.length > 0 && result[0].RightsTopic == 'T')
                    return TopicDao.createTopic(masterPool, insertTopicParams);
                throw {code: 403};
            })
            .then(function () {
                resolve("操作成功");
            }, function (err) {
                reject(err);
            });
    });
};
exports.deleteTopic = function (params) {
    return new Promise(function (resolve,reject) {
        mysqlHelper.getConn()
            .then(function (conn) {
                TopicDao.deleteTopic(conn,params)
                    .then(function () {
                        return CommentDao.deleteComment(conn,params);
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve('删除成功')
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            })
    })
};
exports.addPraise = function (params) {
    return new Promise(function (resolve, reject) {
        mysqlHelper.getConn()
            .then(function (conn) {
                conn.openTransaction()
                    .then(function () {
                        return PraiseDao.createPraise(conn, params);
                    })
                    .then(function () {
                        return TopicDao.addTopicPraise(conn, params);
                    })
                    .then(function (result) {
                        return conn.commitTransaction();
                    }, function (err) {
                        conn.rollbackTransaction();
                        throw err;
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve('点赞成功')
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            });

    });
};
exports.deletePraise = function (params) {
    return new Promise(function (resolve, reject) {
        mysqlHelper.getConn()
            .then(function (conn) {
                conn.openTransaction()
                    .then(function () {
                        return PraiseDao.deletePraise(conn, params);
                    })
                    .then(function () {
                        return TopicDao.reduceTopicPraise(conn, params);
                    })
                    .then(function (result) {
                        return conn.commitTransaction();
                    }, function (err) {
                        conn.rollbackTransaction();
                        throw err;
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve('取消成功')
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            });

    });
};
exports.list = function (params) {
    let _result = {};
    return new Promise(function (resolve, reject) {
        UserDao.queryUser(masterPool, params)
            .then(function (result) {
                params.Province = result[0].Province;
                params.City = result[0].City;
                return TopicDao.selectTopic(masterPool, params, true);
            })
            .then(function (result) {
                _result.topicList = result.data;
                _result.count = result.count;
                let topicIdArr = [];
                for (let i in _result.topicList)
                    topicIdArr.push(_result.topicList[i].TopicId);
                if (topicIdArr.length > 0)
                    return CommentDao.selectComment(masterPool, topicIdArr);
                return [];
            })
            .then(function (result) {
                _result.commentList = result;
                return _result;
            })
            .then(resolve, reject);
    });
};
exports.allList = function (params) {
    let _result = {};
    return new Promise(function (resolve, reject) {
        TopicDao.selectTopic_noLogin(masterPool, params, true)
            .then(function (result) {
                _result.topicList = result.data;
                _result.count = result.count;
                let topicIdArr = [];
                for (let i in _result.topicList)
                    topicIdArr.push(_result.topicList[i].TopicId);
                if (topicIdArr.length > 0)
                    return CommentDao.selectComment(masterPool, topicIdArr);
                return [];
            })
            .then(function (result) {
                _result.commentList = result;
                return _result;
            })
            .then(resolve, reject);
    });
};
exports.pcList = function (params) {
    return new Promise(function (resolve, reject) {
        TopicDao.pcTopicList(masterPool, params)
            .then(resolve, reject);
    });
};
exports.pcDeleteTopic = function (params) {
    return new Promise(function (resolve, reject) {
        TopicDao.pcDeleteTopic(masterPool, params)
            .then(resolve("删除成功"), reject);
    });
};